<?php

class BoardController extends AdminController {

    public function filters() {
        return array(
                'accessControl',
        );
    }
    /*
     * method untuk mendapatkan sponsor push up factory
    */
    public function get_sponsor_board( $member_id ) {
        $network_id = function_lib::get_network_id($member_id);
        $sponsor = function_lib::get_network_sponsor_network_id($network_id);

        do {
            $sponsor_id = $sponsor;
            $status_sponsor = Yii::app()->db->createCommand()
                    ->select('network_level_status')
                    ->from('sys_network')
                    ->where('network_id=:id', array(':id' => $sponsor_id))
                    ->queryScalar();

            if( $status_sponsor == "factory" ) {
                break;
            } else {
                $sponsor = function_lib::get_network_sponsor_network_id($sponsor_id);
            }

        } while( $sponsor != '0' );

        return $sponsor_id;
    }

    /*
     * method input starter class
    */
    public function actionCreate() {

        //
        if( $_POST ) {


            $explode = explode("<br />",nl2br($_POST['data_starter']));
            $i = 0;
            $data_group_cm = $data_group_cd = array();
            foreach ( $explode as $val ) {

                $member_id = ereg_replace('\r\n','',$val);
                $i++;



                if( $_POST['class'] == "1") {
                    //cari sponsornya
                    $spo = $this->get_sponsor_board($val);
                    //cari network_id sponsornya
                    //
                    //cari network_id membernya
                    $network_id = function_lib::get_network_id($val);

                    //masukan id board
                    Yii::app()->db->createCommand("INSERT INTO sys_board_network SET board_network_member_id = '".$network_id."',
                                                                            board_network_network_mid = '".$val."-CM-0001',
                                                                            board_network_board_class = '1'")->execute();
                    $manager_network_id = Yii::app()->db->getLastInsertID();
                    $data_group_cm[] = $manager_network_id;
                    //masukan ke log board
                    Yii::app()->db->createCommand("INSERT INTO sys_board_network_log SET board_network_log_board_network_id = '".$manager_network_id."',
                                                                        board_network_log_input_datetime = '".date("Y-m-d H:i:s")."'")->execute();
                }

                if( $_POST['class'] == "2") {
                    //cari sponsornya
                    $spo = $this->get_sponsor_board($val);
                    //cari network_id sponsornya
                    //
                    //cari network_id membernya
                    $network_id = function_lib::get_network_id($val);

                    //masukan id board
                    Yii::app()->db->createCommand("INSERT INTO sys_board_network SET board_network_member_id = '".$network_id."',
                                                                            board_network_network_mid = '".$val."-CD-0001',
                                                                            board_network_board_class = '2'")->execute();
                    $director_network_id = Yii::app()->db->getLastInsertID();
                    $data_group_cd[] = $director_network_id;
                    //masukan ke log board
                    Yii::app()->db->createCommand("INSERT INTO sys_board_network_log SET board_network_log_board_network_id = '".$manager_network_id."',
                                                                        board_network_log_input_datetime = '".date("Y-m-d H:i:s")."'")->execute();

                }



//                $db->Execute( "UPDATE sys_class_business set class_member_group = REPLACE(class_member_group,'\r\n','') " );
//                $db->Execute( "UPDATE sys_class_first set class_member_group = REPLACE(class_member_group,'\r\n','') " );
//
//                $db->Execute( "UPDATE sys_class_business_network_log set network_log_mid = REPLACE(network_log_mid,'\r\n','') " );
//                $db->Execute( "UPDATE sys_class_first_network_log set network_log_mid = REPLACE(network_log_mid,'\r\n','') " );

            }

            $group_cm = implode(" | ", $data_group_cm);
            $group_cd = implode(" | ", $data_group_cd);

            if( $_POST['class'] == "1") {

                $sql_board1 = 'INSERT INTO sys_board SET board_par_id = \'0\',
                                                 board_leader_board_network_id = \''.$data_group_cm[0].'\',
                                                 board_network_group = \''.$group_cm.'\',
                                                 board_node = 7 ,
                                                 board_start_datetime = NOW(),
                                                 board_class = \'1\'';

                Yii::app()->db->createCommand($sql_board1)->execute();

                $cm_board_id = Yii::app()->db->getLastInsertID();

                //update id board lognya
                foreach( $data_group_cm as $val ) {
                    //$data_group_bc
                    Yii::app()->db->createCommand( "UPDATE sys_board_network_log SET board_network_log_board_id = '$cm_board_id' where board_network_log_board_network_id ='$val'" )->execute();
                }
            }

            if( $_POST['class'] == "2") {

                $sql_board1 = 'INSERT INTO sys_board SET board_par_id = \'0\',
                                                 board_leader_board_network_id = \''.$data_group_cd[0].'\',
                                                 board_network_group = \''.$group_cd.'\',
                                                 board_node = 7 ,
                                                 board_start_datetime = NOW(),
                                                 board_class = \'1\'';
                Yii::app()->db->createCommand($sql_board1)->execute();

                $cd_board_id = Yii::app()->db->getLastInsertID();

                //update id board lognya
                foreach( $data_group_cd as $val ) {
                    //$data_group_bc
                    Yii::app()->db->createCommand( "UPDATE sys_board_network_log SET board_network_log_board_id = '$cd_board_id' where board_network_log_board_network_id ='$val'" )->execute();
                }
            }


        }// end of _post

        $this->render('create_starter_board');
    }
}
